// olorMenu.h : Declaration of the ColorMenu

#ifndef __OLORMENU_H_
#define __OLORMENU_H_

#include "resource.h"       // main symbols
#include <atlctl.h>
#include<Atlbase.h>
#include "ODMenu.h"
#include "Subclass.h"
#include <afxtempl.h>

#include "../include/comhdr.h"

// {BE65B70F-F7B1-42ca-9F3F-D95F2ECEC599}
static const GUID CATID_PROJECT_FRAMEWORK_ADDINS = 
{ 0xbe65b70f, 0xf7b1, 0x42ca, { 0x9f, 0x3f, 0xd9, 0x5f, 0x2e, 0xce, 0xc5, 0x99 } };

#import "..\AddinManager\Debug\AddinManager.tlb" raw_interfaces_only, raw_native_types, no_namespace, named_guids 
/////////////////////////////////////////////////////////////////////////////
// ColorMenu
class ATL_NO_VTABLE ColorMenu : 
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<ColorMenu, &CLSID_olorMenu>,
	public ISupportErrorInfo,
	public IConnectionPointContainerImpl<ColorMenu>,
	public IDispatchImpl<IolorMenu, &IID_IolorMenu, &LIBID_COLORMENUADDINLib>,
	public IDispatchImpl<IFrameworkAddin, &IID_IFrameworkAddin, &LIBID_COLORMENUADDINLib>
{
public:
	ColorMenu()
	{
	}

DECLARE_REGISTRY_RESOURCEID(IDR_OLORMENU)

DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(ColorMenu)
	COM_INTERFACE_ENTRY(IolorMenu)
	COM_INTERFACE_ENTRY2(IDispatch,IolorMenu)
	COM_INTERFACE_ENTRY(ISupportErrorInfo)
	COM_INTERFACE_ENTRY(IConnectionPointContainer)
	COM_INTERFACE_ENTRY(IFrameworkAddin)
END_COM_MAP()
BEGIN_CONNECTION_POINT_MAP(ColorMenu)
END_CONNECTION_POINT_MAP()

BEGIN_CATEGORY_MAP(ColorMenu)
IMPLEMENTED_CATEGORY(CATID_PROJECT_FRAMEWORK_ADDINS)
END_CATEGORY_MAP()

// ISupportsErrorInfo
	STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

// IolorMenu
public:
	IFrameWork* m_pProjectFramework;
	long m_lSessionID;

	STDMETHOD(OnDebugMenu)();
	STDMETHOD(Initialize)(LONG lSessionID, IFrameWork * pApp, VARIANT bFirstTime)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState())

		m_lSessionID = lSessionID;

			CString szModuleName,szModulePath;
		GetModuleFileName(AfxGetInstanceHandle(),szModuleName.GetBufferSetLength(MAX_PATH),MAX_PATH);
		szModuleName.ReleaseBuffer();
		int index = szModuleName.ReverseFind('\\');
		szModulePath = szModuleName.Left(index);
		
		_bstr_t message = L"Loading: " + szModuleName;
		pApp->LoggingService(message);

		CString XMLFileName;
		XMLFileName = szModuleName.Right(szModuleName.GetLength() - index - 1);
		XMLFileName = XMLFileName.Left(XMLFileName.ReverseFind('.'));
		XMLFileName = XMLFileName +  ".xml";
		
		CString szPath = szModulePath + "\\XML\\" + XMLFileName;
		
		CFile xmlfile;
		CFileException ex;
		if(!xmlfile.Open(szPath,CFile::modeRead,&ex))
		{
			TCHAR szError[1024];
			ex.GetErrorMessage(szError, 1024);
			NoticeBox(szError);
			return S_FALSE;
		}
		
		CString strXMLCommandSubFile;
		xmlfile.Read(strXMLCommandSubFile.GetBufferSetLength(xmlfile.GetLength()),xmlfile.GetLength());	
		strXMLCommandSubFile.ReleaseBuffer();
		
		strXMLCommandSubFile.Replace("\n",""); //Replace all new line characters
		strXMLCommandSubFile.Replace("\t",""); //Replace all tab characters
		HINSTANCE lAppHandle=AfxGetInstanceHandle();
		pApp->SetAddInInfo(lSessionID,(long)lAppHandle,strXMLCommandSubFile.AllocSysString(),IDR_TOOLBAR_ADDIN3); 
		m_pProjectFramework=pApp;
		return S_OK;

	}
	HRESULT _stdcall Uninitialize(LONG lSessionID);
	HRESULT _stdcall EndInitialize();
	static BOOL CALLBACK EnumSubingWndProc(HWND hwnd,LPARAM lParam);

private:
	static CODMenu m_ODMenu;
	static CList<CSubclassWnd*,CSubclassWnd*> m_SubWndList;
};

#endif //__OLORMENU_H_
